package com.isay.app.honey.ui.direct;

import android.app.Dialog;
import android.nfc.Tag;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.cash.CashManagerActivity;
import com.isay.app.honey.ui.home.BannerItemInfo;
import com.isay.app.honey.ui.home.HomeItemInfo;
import com.isay.app.honey.ui.home.HomeRecyclerAdapter;
import com.isay.app.honey.ui.profit.MyProfitActivity;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseFragment;
import com.isay.frameworklib.utils.ToastUtils;
import com.isay.frameworklib.utils.WebUrlUtils;
import com.isay.frameworklib.utils.glide.GlideUtils;
import com.isay.frameworklib.widget.GlideImageLoader;
import com.isay.frameworklib.widget.GridItemDecoration;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cywu4 on 2018/8/9.
 */

public class DirectFragment extends BaseFragment implements View.OnClickListener, DirectRecyclerAdapter.OnItemClick {

    private List<BannerItemInfo> mBannerList = new ArrayList<>();

    private String mCountInfo;

    private TextView mTvNotice;
    private TextView mTvCount;
    private Banner mViewBanner;
    private RecyclerView mRecyclerView;
    protected DirectRecyclerAdapter mRecyclerAdapter;

    private ImageView mIvLeft;
    private ImageView mIvRight;
    private ImageView mIvBottom;
    private View mViewLeft;
    private View mViewRight;
    private View mViewBottom;
    private DirectDialog mDirectDialog;


    public static DirectFragment getInstance() {
        return new DirectFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_direct;
    }


    @Override
    protected void initView(View rootView) {
        mTvNotice = rootView.findViewById(R.id.tv_notice);
        mTvNotice.setSelected(true);
        mTvCount= rootView.findViewById(R.id.tv_all_count);
        //RecyclerView
        mRecyclerView = rootView.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        GridItemDecoration divider = new GridItemDecoration.Builder(getContext())
                .setHorizontalSpan(R.dimen.px1)
                .setVerticalSpan(R.dimen.px1)
                .setColorResource(R.color.grey_f2f2f2)
                .setShowLastLine(true)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setLayoutManager(gridLayoutManager);//new GridLayoutManager(getContext(), 3)
        mRecyclerAdapter = new DirectRecyclerAdapter(getActivity(), this);
        mRecyclerView.setAdapter(mRecyclerAdapter);
        //广告位
        mViewLeft = rootView.findViewById(R.id.lay_left);
        mViewLeft.setOnClickListener(this);
        mViewRight = rootView.findViewById(R.id.lay_right);
        mViewRight.setOnClickListener(this);
        mViewBottom = rootView.findViewById(R.id.lay_bottom);
        mViewBottom.setOnClickListener(this);
        rootView.findViewById(R.id.iv_left_clode).setOnClickListener(this);
        rootView.findViewById(R.id.iv_right_clode).setOnClickListener(this);
        rootView.findViewById(R.id.iv_bottom_clode).setOnClickListener(this);
        mIvLeft = rootView.findViewById(R.id.iv_left);
        mIvRight = rootView.findViewById(R.id.iv_right);
        mIvBottom = rootView.findViewById(R.id.iv_bottom);
        //banner
        mViewBanner = rootView.findViewById(R.id.view_banner);
        mViewBanner.setImageLoader(new GlideImageLoader());
        mViewBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (position < mBannerList.size()) {
                    WebUrlUtils.openUrl(getActivity(), mBannerList.get(position).getPath());
                }
            }
        });
    }

    @Override
    protected void initData() {
        query();
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_left:
            case R.id.lay_right:
            case R.id.lay_bottom:
                Object tag = v.getTag();
                if (tag != null) {
                    WebUrlUtils.openUrl(getContext(), tag.toString());
                }
                break;
            case R.id.iv_left_clode:
                mViewLeft.setVisibility(View.GONE);
                break;
            case R.id.iv_right_clode:
                mViewRight.setVisibility(View.GONE);
                break;
            case R.id.iv_bottom_clode:
                mViewBottom.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }


    private void query() {
        NetworkManager.homeDirect(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret == 200) {
                    List<DirectItemInfo> list = new ArrayList<>();
                    String notice = "";
                    BannerItemInfo left = null;
                    BannerItemInfo right = null;
                    BannerItemInfo down = null;
                    int count = 0;
                    try {
                        JSONObject jsonObject = new JSONObject(content);
                        //解析通知
                        notice = jsonObject.optString("notice");
                        //解析count
                        count = jsonObject.optInt("count");
                        //解析提示信息
                        mCountInfo = jsonObject.optString("info");
                        //解析热门下载
                        JSONArray array = jsonObject.optJSONArray("applyList");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object = array.optJSONObject(i);
                            DirectItemInfo info = new DirectItemInfo(object.optString("id"),
                                    object.optString("logo"),
                                    object.optString("name"),
                                    object.optString("num"),
                                    object.optInt("type"));
                            list.add(info);
                        }
                        //解析banner
                        JSONArray bannerArray = jsonObject.optJSONArray("bannerList");
                        for (int i = 0; i < bannerArray.length(); i++) {
                            JSONObject object = bannerArray.optJSONObject(i);
                            BannerItemInfo info = new BannerItemInfo(object.optString("image"),
                                    object.optString("path"));
                            mBannerList.add(info);
                        }
                        //leftadver
                        JSONObject leftObj = jsonObject.optJSONObject("leftadver");
                        left = new BannerItemInfo(leftObj.optString("image"),
                                leftObj.optString("path"));
                        //rightadver
                        JSONObject rightObj = jsonObject.optJSONObject("rightadver");
                        right = new BannerItemInfo(rightObj.optString("image"),
                                rightObj.optString("path"));
                        //downadver
                        JSONObject downObj = jsonObject.optJSONObject("downadver");
                        down = new BannerItemInfo(downObj.optString("image"),
                                downObj.optString("path"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //显示通知
                    mTvNotice.setText(notice);
                    //
                    mTvCount.setText("共" +count+"个");
                    //显示热门下载
                    mRecyclerAdapter.addData(true, list);
                    mRecyclerAdapter.notifyDataSetChanged();
                    //显示banner
                    List<String> bannerUrl = new ArrayList<>();
                    for (BannerItemInfo info : mBannerList) {
                        bannerUrl.add(info.getImage());
                    }
                    mViewBanner.setImages(bannerUrl);
                    mViewBanner.start();
                    //显示广告
                    if (left != null && !TextUtils.isEmpty(left.getImage())) {
                        GlideUtils.loadImageAnim(getContext(), mIvLeft, left.getImage());
                        mViewLeft.setTag(left.getPath());
                        mViewLeft.setVisibility(View.VISIBLE);
                    }
                    if (right != null && !TextUtils.isEmpty(right.getImage())) {
                        GlideUtils.loadImageAnim(getContext(), mIvRight, right.getImage());
                        mViewRight.setTag(right.getPath());
                        mViewRight.setVisibility(View.VISIBLE);
                    }
                    if (down != null && !TextUtils.isEmpty(down.getImage())) {
                        GlideUtils.loadImageAnim(getContext(), mIvBottom, down.getImage());
                        mViewBottom.setTag(down.getPath());
                        mViewBottom.setVisibility(View.VISIBLE);
                    }
                } else {
                    ToastUtils.show(msg);
                }

            }

            @Override
            public void onFailure(Exception e) {
                ToastUtils.show(e.getMessage());
            }
        });
    }


    @Override
    public void onClick(int type) {
        if (mDirectDialog == null) {
            mDirectDialog = new DirectDialog(getContext());
        }
        mDirectDialog.setShowText(type == 1, mCountInfo);
        mDirectDialog.show();
    }


}
